from typing import Any, Dict

from apscheduler.triggers.base import BaseTrigger
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger

from ctaf_core.ctaf_objects.ctaf_general_types import TaskTriggerEnum
from ctaf_core.ctaf_utils.file_process_util import load_json_file


def load_bus_service_sever_settings(bus_service_server_setting_filename: str )->Any:
    """读取配置文件"""
    if not bus_service_server_setting_filename:
        return None
    else:
        return load_json_file(bus_service_server_setting_filename)

def load_local_service_sever_settings(local_service_server_setting_filename: str )->Any:
    """读取配置文件"""
    if not local_service_server_setting_filename:
        return None
    else:
        return load_json_file(local_service_server_setting_filename)

def load_bus_service_client_settings(bus_service_client_setting_filename: str )->Any:
    """读取配置文件"""
    if not bus_service_client_setting_filename:
        return None
    else:
        return load_json_file(bus_service_client_setting_filename)

def load_gateway_settings(gateway_setting_filename: str )->Any:
    """读取配置文件"""
    if not gateway_setting_filename:
        return None
    else:
        return load_json_file(gateway_setting_filename)


def load_quartz_task_settings(quartz_task_setting_filename: str )->Any:
    """读取配置文件"""
    if not quartz_task_setting_filename:
        return None
    else:
        return load_json_file(quartz_task_setting_filename)

def load_db_settings(db_setting_filename: str )->Any:
    """读取配置文件"""
    if not db_setting_filename:
        return None
    else:
        return load_json_file(db_setting_filename)

def exp_to_trigger_instance(trigger_type: str, trigger_exp: Any)->BaseTrigger:
    if TaskTriggerEnum.CRON.value == trigger_type :
        #trigger_exp从左到右这6个位置分别代表秒、分、时、日、月、周
        return CronTrigger.from_crontab(trigger_exp)

    if TaskTriggerEnum.INTERVAL.value == trigger_type :
        # trigger_exp是key-value形式，key=seconds/minutes/hours/days/weeks
        if isinstance(trigger_exp, Dict):
            if trigger_exp["seconds"]:
                return IntervalTrigger(seconds = trigger_exp["seconds"])
            if trigger_exp["minutes"]:
                return IntervalTrigger(minutes = trigger_exp["minutes"])
            if trigger_exp["hours"]:
                return IntervalTrigger(hours = trigger_exp["hours"])
            if trigger_exp["days"]:
                return IntervalTrigger(days = trigger_exp["days"])
            if trigger_exp["weeks"]:
                return IntervalTrigger(weeks = trigger_exp["weeks"])